package me.zhengjie.modules.quartz.task;

import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.constants.PropertyTradeTypeConstants;
import com.qiaofang.applet.service.property.domain.Estate;
import com.qiaofang.applet.service.property.repository.EstateRepository;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.EstateGroupListDto;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class EstateNumBrushTask {

    @Autowired
    private EstateRepository estateRepository;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private RedisUtils redisUtils;


    public void run() {
        String taskKey = RedisKeyUtil.getTaskKey(this.getClass().getSimpleName());
        if (redisUtils.hasKey(taskKey)){
            return;
        }
        redisUtils.set(taskKey, "", 30, TimeUnit.MINUTES);

        log.info("EstateNumBrushTask run start");
        List<EstateGroupListDto> list = propertyService.getEstatePropertyNum();
        Map<String, Integer> saleNumMap = new HashMap<>();
        Map<String, Integer> rentNumMap = new HashMap<>();
        for (EstateGroupListDto estateGroupListDto : list) {
            String estateUniqueUuid = estateGroupListDto.getEstateUniqueUuid();
            String tradeType = estateGroupListDto.getTradeType();
            Integer total = estateGroupListDto.getTotal();
            if (PropertyTradeTypeConstants.ALL.equals(tradeType)) {
                saleNumMap.put(estateUniqueUuid, saleNumMap.get(estateUniqueUuid) == null ? total : saleNumMap.get(estateUniqueUuid) + total);
                rentNumMap.put(estateUniqueUuid, rentNumMap.get(estateUniqueUuid) == null ? total : rentNumMap.get(estateUniqueUuid) + total);
            } else if (PropertyTradeTypeConstants.SALE.equals(tradeType)) {
                saleNumMap.put(estateUniqueUuid, saleNumMap.get(estateUniqueUuid) == null ? total : saleNumMap.get(estateUniqueUuid) + total);
            } else if (PropertyTradeTypeConstants.RENT.equals(tradeType)) {
                rentNumMap.put(estateUniqueUuid, rentNumMap.get(estateUniqueUuid) == null ? total : rentNumMap.get(estateUniqueUuid) + total);
            }
        }
        List<Estate> all = estateRepository.findAll();
        all.forEach(item -> {
            String estateUniqueUuid = item.getEstateUniqueUuid();
            item.setSaleNum(saleNumMap.get(estateUniqueUuid) == null ? 0 : saleNumMap.get(estateUniqueUuid));
            item.setRentNum(rentNumMap.get(estateUniqueUuid) == null ? 0 : rentNumMap.get(estateUniqueUuid));
        });
        if (!CollectionUtils.isEmpty(all)) {
            try {
                estateRepository.saveAll(all);
            } catch (Exception e) {
                log.error("EstateNumBrushTask run error", e);
            }
        }
        log.info("EstateNumBrushTask run end");

        redisUtils.del(taskKey);
    }
}
